var Tag = {
	showRemove:function(tag){
		var $this = $(tag),
		$contents = $this.find('.tag-contents'),
		$remove = $this.find('.tag-remove'),
		originalWidth = $contents.width(),
		width = originalWidth,
		addWidth = $remove.width(),
		timer = setTimeout(function(){
			$remove.fadeOut(100,function(){
				$this.animate({width:originalWidth},100);
				$contents.fadeIn(100);
			});
		},2000);
		
		if(addWidth > width){
			width = addWidth;
		}
		
		$this.animate({width:width},100);
		$contents.fadeOut(100,function(){
			$remove.fadeIn(100);
		});
	},
	remove:function(tagremove){
		var $this = $(tagremove),
		$tag = $this.closest('.fund-tag'),
		value = $tag.attr('value');
		$tag.fadeOut(100,function(){
			$tag.remove();
			Tag.saveSearchTags();
		});
	},
	handleFundClick:function(tag){
		var $tag = $(tag),
		$tagboxinput = $('#tag-box-input'),
		$clone = $tag.clone();
		
		if(!Tag.exists(tag)){
			$clone.hide();
			$tagboxinput.before(' ');
			$tagboxinput.before($clone);
			$clone.fadeIn();
			Tag.saveSearchTags();
		}
	},
	addSuggestion:function(tag){
		var $tag = $(tag),
		$tagboxinput = $('#tag-box-input'),
		$clone = $tag.clone();
		$clone.removeClass('inactive');
		$clone.css('opacity','1');
		$tag.replaceWith($clone);
		Tag.saveSearchTags();
	},
	search:function(){
		var $input = $('#tag-box-input'),
		$tagbox = $input.closest('.tag-box'),
		$inactiveTags = $tagbox.find('.fund-tag.inactive'),
		$activeTags = $tagbox.find('.fund-tag').not('.inactive'),
		activeTags = Tag.getSearchTags(),
		$loading = $('#tag-box-input-loading');
		
		$loading.fadeIn(100);
		Ajax.run("tagView.renderSearchResults",
			function(data){
				$loading.fadeOut(100);
				if($inactiveTags.length > 0){
					$inactiveTags.fadeOut(100,function(){
						$(this).remove();
					});
				}
				$input.before(' ' + data);
				$tagbox.find('.fund-tag.inactive').hide();
				$tagbox.find('.fund-tag.inactive').fadeIn(function(){
					$tagbox.find('.fund-tag.inactive').fadeOut(4000);
				});
				Tag.saveSearchTags();
				if(data.length == 0){
					$('#not-found').show();
					$('#not-found').fadeOut(2000);
				}
			},
			{input:$input.val(),exclude:activeTags}
		);
	},
	clicked:function(tag){
		var isTagbox = false,
		isPopular = false,
		isFund = false,
		$tag = $(tag);
		
		if($tag.closest('.tag-box').length == 1){
			isTagbox = true;
		}
		if($tag.closest('#popular-tag-box').length == 1){
			isPopular = true;
		}
		if($tag.closest('.fund').length == 1){
			isFund = true;
		}
		if(isTagbox){
			Tag.handleTagboxClick(tag);
		}
		if(isFund){
			Tag.handleFundClick(tag);
		}
		if(isPopular){
			Tag.addPopular(tag);
		}
	},
	handleTagboxClick:function(tag){
		var isActive = true,
		$tag = $(tag);
		
		if($tag.hasClass('inactive')){
			isActive = false;
		}
		if(isActive){
			Tag.showRemove(tag);
		} else {
			Tag.addSuggestion(tag);
		}
	},
	getSearchTags:function(delimiter){
		delimiter = delimiter || ',';
		var $tagbox = $('.tag-box'),
		tags="";
		
		$tagbox.find('.fund-tag').not('.inactive').each(function(){
			if(tags != ""){
				tags += delimiter;
			}
			tags += $(this).attr("value");
		});
		return tags.toLowerCase();
	},
	saveSearchTags:function(){
		Cookie.set('tag',Tag.getSearchTags('|'));
		Tag.reflectPopular();
		Fund.throttle(Fund.query,'fundsearch',500);
	},
	addPopular:function(tag){
		var $tag = $(tag),
		$tagboxinput = $('#tag-box-input'),
		$clone = $tag.clone();
		
		//$tag.fadeOut();
		$clone.removeClass('inactive');
		$clone.hide();
		$tagboxinput.before(' ');
		$tagboxinput.before($clone);
		$clone.fadeIn();
		Tag.saveSearchTags();
	},
	getPopularTags:function(){
		var $populars = $('#popular-tag-box'),
		tags="";
		
		$populars.find('.fund-tag').each(function(){
			if(tags != ""){
				tags += ',';
			}
			tags += $(this).attr("value");
		});
		return tags.toLowerCase();
	},
	exists:function(tag){
		var tags = Tag.getSearchTags(),
		tagValue = $(tag).attr('value');
		
		if(tags != ""){
			tags = ','+tags+',';
		}
		return tags.indexOf(tagValue) >= 0;
	},
	reflectPopular:function(){
		var tags = ',' + Tag.getSearchTags() + ',',
		$populars = $('#popular-tag-box');
		
		$populars.find('.fund-tag').each(function(){
			if(tags.indexOf(','+$(this).attr('value')+',') >= 0){ // exists
				if($(this).is(':visible')){
					$(this).fadeOut();
				}
			} else { // not exists
				if(!$(this).is(':visible')){
					$(this).fadeIn();
				}
			}
		});
	}
};